﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using IntelligentEscalator.DAL;
using IntelligentEscalator.ElevatorWebSocket;
using IntelligentEscalator.Models;

namespace IntelligentEscalator.BUS
{
    public class NoticeBUS
    {
        public ResponseMessage AddNotice(NoticeInfo noticeInfo)
        {
            ResponseMessage message = new ResponseMessage();
            NoticeDAL da = new NoticeDAL();
            try
            {
                if (string.IsNullOrEmpty(noticeInfo.updateTime))
                {
                    noticeInfo.updateTime = "null";
                }
                else
                {
                    noticeInfo.updateTime = string.Format("'{0}'", Convert.ToDateTime(noticeInfo.updateTime).ToString("yyyy-MM-dd HH:mm:ss"));
                }

                string result = da.AddNotice(noticeInfo);

                if (result.Contains("success"))
                {
                    message.errcode = 0;
                    message.errmsg = "";
                    message.data = null;
                    message.total = 1;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "新增通知公告失败";
                    message.data = null;
                    message.total = 0;
                }
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "新增通知公告异常";
                message.data = null;
                message.total = 0;
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "新增通知公告";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
                LogHelper.Log("新增通知公告" + e.ToString());
            }
            return message;
        }

        public ResponseMessage EditNotice(NoticeInfo noticeInfo)
        {
            ResponseMessage message = new ResponseMessage();
            NoticeDAL da = new NoticeDAL();
            if (string.IsNullOrEmpty(noticeInfo.updateTime))
            {
                noticeInfo.updateTime = "null";
            }
            else
            {
                noticeInfo.updateTime = string.Format("'{0}'", Convert.ToDateTime(noticeInfo.updateTime).ToString("yyyy-MM-dd HH:mm:ss"));
            }

            string result = da.EditNotice(noticeInfo);

            if (result.Contains("success"))
            {
                message.errcode = 0;
                message.errmsg = "";
                message.data = null;
                message.total = 1;
            }
            else
            {
                message.errcode = 1;
                message.errmsg = "修改通知公告失败";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        public ResponseMessage DelNotice(int noticeid)
        {
            ResponseMessage message = new ResponseMessage();
            NoticeDAL da = new NoticeDAL();
            string result = string.Empty;
            try
            {
                result = da.DelNotice(noticeid);

                if (result.Contains("success"))
                {
                    message.errcode = 0;
                    message.errmsg = "";
                    message.data = null;
                    message.total = 1;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = result;
                    message.data = null;
                    message.total = 0;
                }
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = e.ToString();
                message.data = null;
                message.total = 0;
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "删除通知公告";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
                LogHelper.Log("删除通知公告" + e.ToString());
            }
            return message;
        }
        public ResponseMessage QueryNotice(NoticeInfo noticeInfo)
        {
            ResponseMessage message = new ResponseMessage();
            NoticeDAL da = new NoticeDAL();
            try
            {
                List<NoticeInfo> content = da.QueryNoticeList(noticeInfo);

                message.errcode = 0;
                message.errmsg = "";
                message.data = content;
                message.total = da.QueryNoticeListConut(noticeInfo);
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = e.ToString();
                message.data = null;
                message.total = 0;
            }

            return message;
        }

        public ResponseMessage LoadNotice(int noticeid)
        {
            ResponseMessage message = new ResponseMessage();
            NoticeDAL da = new NoticeDAL();
            try
            {
                NoticeInfo content = da.LoadNotice(noticeid);
                if (content != null)
                {
                    message.errcode = 0;
                    message.errmsg = "";
                    message.data = content;
                    message.total = 1;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "未查询到相关信息";
                    message.data = null;
                    message.total = 0;
                }
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = e.ToString();
                message.data = null;
                message.total = 0;
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "载入通知公告";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
                LogHelper.Log("载入通知公告" + e.ToString());
            }
            return message;
        }

        public ResponseMessage ToppingNotice(NoticeInfo noticeInfo)
        {
            ResponseMessage message = new ResponseMessage();
            NoticeDAL da = new NoticeDAL();
            try
            {
                string result = da.ToppingNotice(noticeInfo);

                if (result.Contains("success"))
                {
                    bool result2 = PushNoticeMessage("Refresh").Result;
                    message.errcode = 0;
                    message.errmsg = "";
                    message.data = null;
                    message.total = 1;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "上架通知公告失败";
                    message.data = null;
                    message.total = 0;
                }
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = e.ToString();
                message.data = null;
                message.total = 0;
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "上架通知公告";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
                LogHelper.Log("上架通知公告" + e.ToString());
            }
            return message;
        }

        public async Task<bool> PushNoticeMessage(string message)
        {
            return await ElevatorWebSocketMiddleware.webSocketManage.PushNoticeMessage(message);
        }

        public ResponseMessage BatchDelNotice(string ids)
        {
            ResponseMessage message = new ResponseMessage();
            NoticeDAL da = new NoticeDAL();
            string result = string.Empty;
            try
            {
                result = da.BatchDelNotice(ids);

                if (result.Contains("success"))
                {
                    message.errcode = 0;
                    message.errmsg = "";
                    message.data = null;
                    message.total = 1;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = result;
                    message.data = null;
                    message.total = 0;
                }
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = e.ToString();
                message.data = null;
                message.total = 0;
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "批量删除通知公告";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
                LogHelper.Log("批量删除通知公告" + e.ToString());
            }
            return message;
        }
    }
}
